package com.yjtc.cbg.commonutildemo.View;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.yjtc.cbg.commonutildemo.R;
import com.yjtc.cbg.commonutildemo.Util.SharePreferences;

/**
 * Title: CommonUtilDemo
 * <p>
 * Description:
 * <p>
 * Author:baigege (baigegechen@gmail.com)
 * <p>
 * Date:2017-03-23
 */
public class TestActivity extends Activity {

    EditText numbuter, password;
    CheckBox checkBox;
    SharePreferences a;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    private void init() {
        numbuter = (EditText) findViewById(R.id.number);
        password = (EditText) findViewById(R.id.password);
        checkBox = (CheckBox) findViewById(R.id.checkbox);
        a = new SharePreferences();
        numbuter.addTextChangedListener(new TextWatcher() {
                                            @Override
                                            public void onTextChanged(CharSequence s, int start, int before, int count) {
                                                String number = numbuter.getText().toString();
                                                String pwd = a.duqu(TestActivity.this, number);
                                                boolean b = a.duqucheck(TestActivity.this, number);
                                                if (pwd != null && b != false) {
                                                    password.setText(pwd);
                                                } else
                                                    password.setText("");
                                            }

                                            @Override
                                            public void afterTextChanged(Editable s) {
                                            }

                                            @Override
                                            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                                            }
                                        }
        );

    }


    public void button(View view) {
        String numb = numbuter.getText().toString();
        String pass = password.getText().toString();
        String pwd = a.duqu(TestActivity.this, numb);
        if (numb.equals("") || pass.equals("")) {
            Toast.makeText(TestActivity.this, "密码或账号不能为空！", Toast.LENGTH_SHORT).show();
        } else if (!pwd.equals(pass)) {
            Toast.makeText(TestActivity.this, "账号或密码错误！", Toast.LENGTH_LONG).show();
            Intent intent = new Intent(this, TestActivity.class);
            startActivity(intent);
        } else if (pwd.equals(pass)) {
            Toast.makeText(TestActivity.this, "登录成功！", Toast.LENGTH_LONG).show();
        }
    }

    public void button2(View view) {
        String numb = numbuter.getText().toString();
        String pass = password.getText().toString();
        if (pass.equals("") || numb.equals("")) {
            Toast.makeText(TestActivity.this, "账号或密码不能为空", Toast.LENGTH_LONG).show();
        } else if (!pass.equals("") && !numb.equals("") && checkBox.isChecked()) {
            a.baocun(TestActivity.this, numb, pass);
            Toast.makeText(TestActivity.this, "注册成功", Toast.LENGTH_LONG).show();
            numbuter.setText("");
            password.setText("");
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
